home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / rclesrc.10 / ircle sources / CTCP.p < prev    next >
Encoding:
Text File  |  1992-09-04  |  4.3 KB  |  188 lines

  1. {    ircle - Internet Relay Chat client    }
  2. {    File: CTCP    }
  3. {    Copyright ⌐ 1992 Olaf Titz (s_titz@iravcl.ira.uka.de)    }
  4.  
  5. {    This program is free software; you can redistribute it and/or modify    }
  6. {    it under the terms of the GNU General Public License as published by    }
  7. {    the Free Software Foundation; either version 2 of the License, or    }
  8. {    (at your option) any later version.    }
  9.  
  10. {    This program is distributed in the hope that it will be useful,    }
  11. {    but WITHOUT ANY WARRANTY; without even the implied warranty of    }
  12. {    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    }
  13. {    GNU General Public License for more details.    }
  14.  
  15. {    You should have received a copy of the GNU General Public License    }
  16. {    along with this program; if not, write to the Free Software    }
  17. {    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.    }
  18.  
  19. unit CTCP;
  20. { Handles CTCP messages }
  21.  
  22. interface
  23. uses
  24.     TCPTypes, TCPStuff, TCPConnections, MsgWindows,{}
  25.     IRCGlobals, IRCaux, IRCChannels, IRCCommands, DCC;
  26.  
  27. procedure doCTCP (var from, s: string);
  28. { This handles possible CTCP messages - return empty if processed }
  29.  
  30. implementation
  31. { Special thanks to Klaus Zeuge for providing protocol documentation }
  32.  
  33. var
  34.     comm, rest: str255;
  35.  
  36. procedure quote (var s: string);
  37.     var
  38.         i: integer;
  39.     begin
  40.         i := 1;
  41.         while i <= length(s) do begin
  42.             case ord(s[i]) of
  43.                 0: 
  44.                     begin
  45.                     s[i] := chr(16);
  46.                     insert('0', s, i + 1);
  47.                 end;
  48.                 10: 
  49.                     begin
  50.                     s[i] := chr(16);
  51.                     insert('n', s, i + 1);
  52.                 end;
  53.                 13: 
  54.                     begin
  55.                     s[i] := chr(16);
  56.                     insert('r', s, i + 1);
  57.                 end;
  58.                 16: 
  59.                     begin
  60.                     i := i + 1;
  61.                     s[i] := chr(16);
  62.                     insert(chr(16), s, i);
  63.                 end;
  64.                 otherwise
  65.                     begin
  66.                 end;
  67.             end;
  68.             i := i + 1;
  69.         end;
  70.     end;
  71.  
  72.  
  73. procedure unquote (var s: string);
  74.     var
  75.         i: integer;
  76.     begin
  77.         repeat
  78.             i := pos(chr(16), s);
  79.             if i = 0 then
  80.                 leave;
  81.             if i = length(s) then
  82.                 s[0] := chr(i - 1)
  83.             else begin
  84.                 case ord(s[i + 1]) of
  85.                     48: { 0 }
  86.                         s[i + 1] := chr(0);
  87.                     110:  { n }
  88.                         s[i + 1] := chr(10);
  89.                     114:  { r }
  90.                         s[i + 1] := chr(13);
  91.                     16: { Ctrl-P }
  92.                         s[i + 1] := chr(16);
  93.                     otherwise
  94.                         begin
  95.                     end;
  96.                 end;
  97.                 delete(s, i, 1);
  98.             end;
  99.         until false;
  100.     end;
  101.  
  102. procedure CTCPComm (var fr: string; var co, re: str255);
  103.     var
  104.         tt: longint;
  105.         i: integer;
  106.         st: string;
  107.     procedure reply;
  108.         begin
  109.             quote(st);
  110.             st := concat('NOTICE ', fr, ' ', chr(1), co, ' ', st, chr(1));
  111.             if serverStatus = 0 then
  112.                 PutLine(st);
  113.         end;
  114.     begin
  115.         UprString(co, false);
  116.         if co = 'ACTION' then begin
  117.             st := concat(fr, ' ', re);
  118.             Message(st)
  119.         end
  120.         else if co = 'CLIENTINFO' then begin
  121.             i := pos(' ', re);
  122.             if i > 0 then
  123.                 re[0] := chr(i - 1);
  124.             UprString(re, false);
  125.             if re = 'ACTION' then
  126.                 st := 'ACTION contains action descriptions for atmosphere'
  127.             else if re = 'CLIENTINFO' then
  128.                 st := 'CLIENTINFO gives information about available CTCP commands'
  129.             else if re = 'DCC' then
  130.                 st := 'DCC requests a direct client connection'
  131.             else if re = 'ERRMSG' then
  132.                 st := 'ERRMSG returns error messages'
  133.             else if re = 'FINGER' then
  134.                 st := 'FINGER shows login name and idle time of user'
  135.             else if re = 'VERSION' then
  136.                 st := 'VERSION shows information about client version'
  137.             else
  138.                 st := 'ACTION CLIENTINFO DCC ERRMSG FINGER VERSION :Use CLIENTINFO <command> to get information about specific command';
  139.             reply;
  140.         end
  141.         else if co = 'DCC' then
  142.             DCCrequest(fr, re)
  143.         else if co = 'ERRMSG' then begin
  144.             st := concat(re, ' :no error');
  145.             reply
  146.         end
  147.         else if co = 'FINGER' then begin
  148.             getdatetime(tt);
  149.             st := stringof(re, ' :', default^^.userLoginName, ' :idle ', abs(tt - idleTime) : 1, ' seconds');
  150.             reply
  151.         end
  152.         else if co = 'VERSION' then begin
  153.             st := concat('ircle :', CL_VERSION, ' :Apple Macintosh (tm)');
  154.             reply
  155.         end
  156.         else begin
  157.             st := concat('*** Unknown CTCP from ', fr, ': ', co, ' ', re);
  158.             Message(st);
  159.             st := concat(co, ' :unknown query');
  160.             co := 'ERRMSG';
  161.             reply
  162.         end;
  163.     end;
  164.  
  165. procedure doCTCP (var from, s: string);
  166.     var
  167.         i, j, k: integer;
  168.     begin
  169.         repeat
  170.             i := pos(chr(1), s);
  171.             if i = 0 then
  172.                 leave;
  173.             comm := copy(s, i + 1, 255);
  174.             j := pos(chr(1), comm);
  175.             if j = 0 then
  176.                 j := 254;
  177.             k := pos(' ', comm);
  178.             if k = 0 then
  179.                 k := j;
  180.             rest := copy(comm, k + 1, j - k - 1);
  181.             comm[0] := chr(k - 1);
  182.             delete(s, i, j + 1);
  183.             unquote(rest);
  184.             CTCPComm(from, comm, rest);
  185.         until false;
  186.     end;
  187.  
  188. end.